﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using System.IO;
using Telerik.WinControls.UI;
using Telerik.WinControls.Data;
using Telerik.WinControls.Enumerations;
using log4net;
//using System.Collections;
using Bytescout.BarCode;

namespace QuanLyNhaSach
{
    public partial class frmTienIch_InMaVach : Form
    {
        //Tao Parttern Seleton
        static frmTienIch_InMaVach doituong = null;
        public static frmTienIch_InMaVach TaoDoiTuong()
        {
            if (doituong == null)
                doituong = new frmTienIch_InMaVach();
            return doituong;
        }

        public frmTienIch_InMaVach()
        {
            InitializeComponent();
        }
        #region "Khai Báo Biến Toàn Cục"
        sv_QuanLyNhaSach.AdapterClient xuly;
        private ILog logger;        
        Barcode barcode = new Barcode();
        #endregion

        private void frmTienIch_InMaVach_Load(object sender, EventArgs e)
        {                      
            try
            {
                xuly = new sv_QuanLyNhaSach.AdapterClient();
                logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                LoadMatHang();
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }

        }

        private void LoadMatHang()
        {
            try
            {
                RadMultiColumnComboBoxElement multiColumnComboElement = this.mtb_mahang.MultiColumnComboBoxElement;
                multiColumnComboElement.DropDownSizingMode = SizingMode.UpDownAndRightBottom;
                multiColumnComboElement.DropDownMinSize = new Size(300, 200);

                DataSet ds_MatHang = new DataSet();
                ds_MatHang.ReadXml(new StringReader(xuly.LayDS_MatHang()));
                if (ds_MatHang.Tables.Count > 0)
                {
                    mtb_mahang.DataSource = ds_MatHang.Tables[0];
                    mtb_mahang.DisplayMember = "TenMH";
                    mtb_mahang.ValueMember = "MaMH";
                    mtb_mahang.SelectedIndex = -1;
                }
                FilterDescriptor des_mh = new FilterDescriptor(this.mtb_mahang.DisplayMember, FilterOperator.StartsWith, string.Empty);
                mtb_mahang.EditorControl.FilterDescriptors.Add(des_mh);
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }

        }

        int stt = 0;
        int vitri;
        private void bt_chuyenxuong_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_ma.Text.Length == 0)
                    MessageBox.Show("Bạn chưa chọn mặt hàng để thêm xuống lưới");
                else
                {
                    if (gv_thongtin.Rows.Count > 0)
                    {
                        for (int i = 0; i < gv_thongtin.Rows.Count; i++)
                        {
                            if (txt_ma.Text.Trim() == gv_thongtin.Rows[i].Cells["MaMH"].Value.ToString().Trim())
                            {
                                gv_thongtin.Rows[i].Cells["SoLuongIn"].Value = (int.Parse(gv_thongtin.Rows[i].Cells["SoLuongIn"].Value.ToString()) + int.Parse(txt_soluongin.Text.Trim())).ToString();
                                return;
                            }
                        }
                    }
                    //tang stt len 1
                    stt += 1;
                    //gan gia tri cho bien
                    if (gv_thongtin.Rows.Count != 0)
                        vitri = gv_thongtin.Rows.Count;
                    else
                        vitri = 0;
                    //them thong tin vao gridview
                    gv_thongtin.Rows.AddNew();
                    gv_thongtin.Rows[vitri].Cells["STT"].Value = stt;
                    gv_thongtin.Rows[vitri].Cells["SoLuongIn"].Value = int.Parse(txt_soluongin.Text);
                    DataSet ds_ThongTinSachDaChon = new DataSet();
                    ds_ThongTinSachDaChon.ReadXml(new StringReader(xuly.selectqueryByText("select MaMH,TenMH,DVT,DonGiaLe,MaLoai from MatHang where MaMH = '" + mtb_mahang.SelectedValue.ToString() + "' ")));
                    if (ds_ThongTinSachDaChon.Tables.Count > 0)
                    {
                        for (int i = 0; i < ds_ThongTinSachDaChon.Tables[0].Rows.Count; i++)
                        {
                            ds_ThongTinSachDaChon.Tables[0].Rows[i]["DonGiaLe"] = double.Parse(ds_ThongTinSachDaChon.Tables[0].Rows[i]["DonGiaLe"].ToString()).ToString("#,##0");
                        }
                        if (mtb_mahang.SelectedIndex != -1)
                        {                            
                            gv_thongtin.Rows[vitri].Cells["MaMH"].Value = ds_ThongTinSachDaChon.Tables[0].Rows[0]["MaMH"].ToString();
                            gv_thongtin.Rows[vitri].Cells["TenMH"].Value = ds_ThongTinSachDaChon.Tables[0].Rows[0]["TenMH"].ToString();                            
                            gv_thongtin.Rows[vitri].Cells["DVT"].Value = ds_ThongTinSachDaChon.Tables[0].Rows[0]["DVT"].ToString();
                            gv_thongtin.Rows[vitri].Cells["DonGiaLe"].Value = ds_ThongTinSachDaChon.Tables[0].Rows[0]["DonGiaLe"].ToString();
                            gv_thongtin.Rows[vitri].Cells["MaLoai"].Value = ds_ThongTinSachDaChon.Tables[0].Rows[0]["MaLoai"].ToString();
                        }
                    }
                    //Xoa bo thong tin trong cac o trong                    
                    LoadMatHang();
                    txt_ma.Text = "";
                    mtb_mahang.Text = "";
                    txt_soluongin.Text = "1";
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }

        }

        private void mtb_mahang_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (mtb_mahang.Text == "")
                {
                    txt_ma.Text = "";
                }
                else
                {
                    txt_ma.Text = mtb_mahang.SelectedValue.ToString();
                    //DataSet ds_ThongTinSachDaChon = new DataSet();
                    //ds_ThongTinSachDaChon.ReadXml(new StringReader(xuly.selectqueryByText("select MaMH from MatHang where MaMH = '" + mtb_mahang.SelectedValue.ToString() + "' ")));
                    //if (ds_ThongTinSachDaChon.Tables.Count > 0)
                    //{
                    //    if (mtb_mahang.SelectedIndex != -1)
                    //    {
                    //        txt_ma.Text = ds_ThongTinSachDaChon.Tables[0].Rows[0][0].ToString();
                    //    }
                    //}
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }

        }

        private void bt_xemin_Click(object sender, EventArgs e)
        {
            try
            {
                barcode.DrawToNewPDF("C:\\QR_codeMau.pdf", 500, 500, 100, 100);
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo.FileName = "C:\\QR_codeMau.pdf";
                //p.StartInfo.Arguments = "Lệnh cần thực hiện";
                p.Start();

                //if (gv_thongtin.RowCount > 0)
                //{
                //    maLoai = gv_thongtin.CurrentRow.Cells["MaLoai"].Value.ToString().Trim();
                //    if (maLoai == "SA")
                //    {
                //        maMH = gv_thongtin.CurrentRow.Cells["MaMH"].Value.ToString().Substring(0, 4).Trim();
                //        soMaSP = gv_thongtin.CurrentRow.Cells["MaMH"].Value.ToString().Substring(4, 16).Trim();
                //    }
                //    else
                //    {
                //        maMH = gv_thongtin.CurrentRow.Cells["MaMH"].Value.ToString().Substring(0, 2);
                //        soMaSP = gv_thongtin.CurrentRow.Cells["MaMH"].Value.ToString().Substring(2, 16).Trim();
                //    }
                //    //Chuyen doi Chu Cai sang So trong ma ASCII
                //    foreach (char c in maMH)
                //    {
                //        maMHTam += Convert.ToInt32(c).ToString();
                //    }

                //    //System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
                //    System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
                //    pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.pd_PrintPage);
                //    pd.Print();
                //}
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }

        //private void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs ev)
        //{                                    
        //    TaoBarCode_Ean13(maMHTam, soMaSP);
        //    tao_BarCode.Scale = float.Parse("1");
        //    tao_BarCode.DrawEan13Barcode(ev.Graphics, new System.Drawing.Point(25, 25));
        //    ev.Graphics.Dispose();
        //}

        string maMH = "";
        string maLoai = "";
        string maMHTam = "";
        string soMaSP = "";
        private void btn_XemBarCode_Click(object sender, EventArgs e)
        {
            try
            {
                barcode.Symbology = Bytescout.BarCode.SymbologyType.QRCode;
                barcode.Value = gv_thongtin.CurrentRow.Cells["MaMH"].Value.ToString().Trim() + "*http://localhost:11908/TrangChu.aspx*";
                barcode.AdditionalCaption = gv_thongtin.CurrentRow.Cells["MaMH"].Value.ToString().Trim() + "\n" + double.Parse(gv_thongtin.CurrentRow.Cells["DonGiaLe"].Value.ToString()).ToString("#,##0 VNĐ");
                barcode.AddChecksumToCaption = true;
                barcode.SetAdditionalCaptionFont("Arial", 14);
                barcode.AdditionalCaptionPosition = CaptionPosition.Below;
                barcode.NarrowBarWidth = 4;
                //barcode.AddChecksumToCaption = true;
                barcode.DrawQuietZones = true;
                picBarcode.Image = barcode.GetImage();
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }        

        private void bt_trove_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void gv_thongtin_CommandCellClick(object sender, EventArgs e)
        {
            gv_thongtin.CurrentRow.Delete();
        }
    }
}
